package DTO;

import java.awt.List;
import java.io.Serializable;
import java.sql.NClob;
import java.util.Vector;

//import BUS.QueryProccessor;
//import DAO.ColInfoDAO;
//import DAO.DatabaseDAO;
//import DAO.RecordDAO;
import GUI.MainScreen;

public class TableDTO implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3760122950644506512L;
	private String _name;
	private Vector<RecordDTO> _recordList;
	private Vector<ColInfoDTO> _colInfoList;

	public TableDTO() {

	}

	public TableDTO(String name, Vector<ColInfoDTO> colInfoList,
			Vector<RecordDTO> recordList) {
		_name = name;
		_recordList = recordList;
		_colInfoList = colInfoList;
	}

	public TableDTO(TableDTO table) {
		_name = new String(table._name);
		_recordList = new Vector<RecordDTO>();
		for (int i = 0; i < _recordList.size(); i++)
			_recordList.add(new RecordDTO(table._recordList.elementAt(i)));

		for (int i = 0; i < _colInfoList.size(); i++)
			_colInfoList.add(new ColInfoDTO(table._colInfoList.elementAt(i)));
	}
	
	public void set_name(String _name) {
		this._name = _name;
		_recordList = new Vector<RecordDTO>();
		_colInfoList = new Vector<ColInfoDTO>();
	}

	public String get_name() {
		return _name;
	}

	public void set_recordList(Vector<RecordDTO> _recordList) {
		this._recordList = _recordList;
	}

	public Vector<RecordDTO> get_recordList() {
		return _recordList;
	}

	public void set_colInfoList(Vector<ColInfoDTO> _colInfoList) {
		this._colInfoList = _colInfoList;
	}

	public Vector<ColInfoDTO> get_colInfoList() {
		return _colInfoList;
	}
/*
	public TableDTO mul(TableDTO table) {
		// TODO Auto-generated method stub
		TableDTO result = null;

		// mul colInfo
		Vector<ColInfoDTO> colInfoResult = new Vector<ColInfoDTO>();
		for (int i = 0; i < this._colInfoList.size(); i++) {
			colInfoResult.add(this._colInfoList.elementAt(i));
		}
		for (int i = 0; i < table._colInfoList.size(); i++) {
			colInfoResult.add(table._colInfoList.elementAt(i));
		}

		// mul recordList
		Vector<RecordDTO> recordInfoResult = new Vector<RecordDTO>();
		for (int i = 0; i < this._recordList.size(); i++) {
			for (int j = 0; j < table._recordList.size(); j++) {
				recordInfoResult.add(this._recordList.elementAt(i).mul(
						table._recordList.elementAt(j)));
			}
		}
		result = new TableDTO(this._name + "_" + table._name, colInfoResult,
				recordInfoResult);
		return result;
	}

	public TableDTO FilterWhere(String whereInput, String fromInput, TableDTO table1,
			TableDTO table2, Vector<String> tableNames,
			Vector<String> tableVariables) throws Exception {
		// TODO Auto-generated method stub
		TableDTO result = null;

		// process WHERE
		Vector<String> whereCols = new Vector<String>();
		Vector<String> whereVals = new Vector<String>();
		Vector<Boolean> operatorOrder = new Vector<Boolean>();
		QueryProccessor.processConditionString(whereInput, whereCols,
				whereVals, operatorOrder);

		// loc record
		DatabaseDAO databaseDAO = new DatabaseDAO(MainScreen._DATABASE_PATH);
		DatabaseDTO databaseDTO = databaseDAO.getDatabase();
		Vector<RecordDTO> recordList = new Vector<RecordDTO>();
		for (int i = 0; i < this._recordList.size(); i++) {
			RecordDTO record = this._recordList.elementAt(i);
			Vector<Boolean> listBoolean = new Vector<Boolean>();
			if (whereCols.size() > 0) {
				for (int z = 0; z < whereCols.size(); z++) {
					int colIndexTable1 = databaseDTO.getColIndex(
							whereCols.elementAt(z), tableNames, tableVariables, fromInput);
					int colIndexTable2 = databaseDTO.getColIndex(
							whereVals.elementAt(z), tableNames, tableVariables, fromInput);
					String valuesOfWhereCols= null; 
					String valuesOfWhereVals=null;
					if(colIndexTable1 != -1)
						valuesOfWhereCols = record.get_data()[colIndexTable1];
					else 
						valuesOfWhereCols = whereVals.elementAt(z).replaceAll("'", "");
					if(colIndexTable2 != -1)
						valuesOfWhereVals = record.get_data()[table1._colInfoList.size() + colIndexTable2];
					else 
						valuesOfWhereVals = whereVals.elementAt(z).replaceAll("'", "");
					listBoolean.add(valuesOfWhereCols.equals(valuesOfWhereVals));
				}
			}
			boolean flag = true;
			if (listBoolean.size() > 0) {
				flag = listBoolean.elementAt(0);
				int index = 0;
				for (int j = 1; j < listBoolean.size(); j++)
					if (operatorOrder.elementAt(index++))
						flag = flag || listBoolean.elementAt(j);
					else
						flag = flag && listBoolean.elementAt(j);
			}
			if (flag) {
				recordList.add(record);
			}
		}
		return new TableDTO("", _colInfoList, recordList);
	}

	public int getColIndex(String string) {
		// TODO Auto-generated method stub
		for (int i = 0; i < _colInfoList.size(); i++)
			if (_colInfoList.elementAt(i).get_colName().equals(string))
				return i;
		return -1;
	}

	public TableDTO FilterSelect(String selectInput, String fromInput, TableDTO table1,
			TableDTO table2, Vector<String> tableNames,
			Vector<String> tableVariables) throws Exception {
		// TODO Auto-generated method stub
		TableDTO result = null;
		selectInput = selectInput.trim();
		if (selectInput.equals("*"))
			return this;
		else {
			DatabaseDAO databaseDAO = new DatabaseDAO(MainScreen._DATABASE_PATH);
			DatabaseDTO databaseDTO = databaseDAO.getDatabase();
			Vector<Integer> listIndex = new Vector<Integer>();
			String[] temp = selectInput.split(",");
			// select x.x, y.z from xxx x, yyy y where x.x = y.x
			for (int i = 0; i < temp.length; i++) {
				int index = temp[i].indexOf(".");
				String[] tmp = new String[2];
				tmp[0] = temp[i].substring(0, index).trim();
				tmp[1] = temp[i].substring(index+1, temp[i].length()).trim();
				int indexCol = databaseDTO.getColIndex(temp[i], tableNames,
						tableVariables, fromInput);
				String tblName = FindTableNameIn(tmp[0], tableNames, tableVariables);
				if (tblName.equals(table1.get_name()))
					listIndex.add(indexCol);
				else
					listIndex.add(indexCol + table1._colInfoList.size());
			}

			Vector<ColInfoDTO> colInfo = new Vector<ColInfoDTO>();
			Vector<RecordDTO> recordInfo = new Vector<RecordDTO>();
			for (int i = 0; i < listIndex.size(); i++) {
				colInfo.add(this._colInfoList.elementAt(i));
			}
			for (int i = 0; i < this._recordList.size(); i++){
				String []data = new String[listIndex.size()];
				for (int j = 0; j < listIndex.size(); j++) {					
					data[j] = this._recordList.elementAt(i).get_data()[listIndex.elementAt(j)];
				}
				recordInfo.add(new RecordDTO(listIndex.size(), data));
			}
			result = new TableDTO("", colInfo, recordInfo);
		}
		return result;

	}
	
	private String FindTableNameIn(String tableName,Vector<String> tableNames,
			Vector<String> tableVariables) {
		// TODO Auto-generated method stub
		for(int i = 0;i<tableVariables.size();i++)
			if(tableVariables.elementAt(i).equals(tableName))
				return tableNames.elementAt(i);
		return tableName;
	}
*/
}
